/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.config.web.message;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.config.entity.message.ConfigMessage;
import com.thinkgem.jeesite.modules.config.service.message.ConfigMessageService;

/**
 * 短信配置Controller
 *
 * @author JiangYongKang
 * @version 2016-12-30
 */
@Controller
@RequestMapping(value = "${adminPath}/config/message/configMessage")
public class ConfigMessageController extends BaseController {

    @Autowired
    private ConfigMessageService configMessageService;

    @ModelAttribute
    public ConfigMessage get(@RequestParam(required = false) String id) {
        ConfigMessage entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = configMessageService.get(id);
        }
        if (entity == null) {
            entity = new ConfigMessage();
        }
        return entity;
    }

    @RequiresPermissions("config:message:configMessage:view")
    @RequestMapping(value = {"list", ""})
    public String list(ConfigMessage configMessage, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ConfigMessage> page = configMessageService.findPage(new Page<ConfigMessage>(request, response), configMessage);
        model.addAttribute("page", page);
        return "modules/config/message/configMessageList";
    }

    @RequiresPermissions("config:message:configMessage:view")
    @RequestMapping(value = "form")
    public String form(ConfigMessage configMessage, Model model) {
        model.addAttribute("configMessage", configMessage);
        return "modules/config/message/configMessageForm";
    }

    @RequiresPermissions("config:message:configMessage:edit")
    @RequestMapping(value = "save")
    public String save(ConfigMessage configMessage, Model model, RedirectAttributes redirectAttributes) {
        configMessageService.save(configMessage);
        addMessage(redirectAttributes, "保存短信配置成功");
        return "redirect:" + Global.getAdminPath() + "/config/message/configMessage/?repage";
    }

    @RequiresPermissions("config:message:configMessage:edit")
    @RequestMapping(value = "delete")
    public String delete(ConfigMessage configMessage, RedirectAttributes redirectAttributes) {
        configMessageService.delete(configMessage);
        addMessage(redirectAttributes, "删除短信配置成功");
        return "redirect:" + Global.getAdminPath() + "/config/message/configMessage/?repage";
    }

}